# 努力学习
# 比肩漆时祺和周期许
# import pymysql
#
# conn=pymysql.connect(host='192.168.0.41',port=3306,user='root',password='888888',database='cjglxt',charset='utf8')
# # conn=pymysql.connect(host="192.168.0.41",port=3306,user="root",password="888888",database="cjglxt",charset="utf8")
#
# cursor=conn.cursor()
# cursor.execute("insert into xy() values(2,'xx');")
# # result=cursor.fetchall()
# print('影响的行数：',conn.affected_rows())
# conn.commit()
# # print(result)
# cursor.close()
# conn.close()

import pymysql
class MysqlTill():
    conn = None
    @classmethod
    def __get_conn(cls):
        if cls.conn is None:
            cls.conn=pymysql.connect(host='',port=3306,user='root',password='888888',database='vue_api_server',charset='utf8')
        return cls.conn
    @classmethod
    def __close_conn(cls):
        if cls.conn is not None:
            cls.conn.close()
            cls.conn=None
    @classmethod
    def select_sql(cls,sql):
        try:
            cls.conn=cls.__get_conn()
            cursor=cls.conn.cursor()
            cursor.execute(sql)
            result=cursor.fetchall()
            print(result)
        except Exception as e:
            print('查询结果出错：',e)
        finally:
            cursor.close()
            cls.__close_conn()
    @classmethod
    def uid_sql(cls,sql):
        try:
            cls.conn = cls.__get_conn()
            cursor = cls.conn.cursor()
            cursor.execute(sql)
            result = cursor.fetchall()
            print('影响的行数：',cls.conn.affected_rows())
            # logging.info(f'影响的行数,{cls.conn.affected_rows()}')
        except Exception as e:
            print('增删改语句出错：',e)
            cls.conn.rollback()
        else:
            cls.conn.commit()
        finally:
            cursor.close()
            cls.__close_conn()
# if __name__ == '__main__':
#     MysqlTill.select_sql('select * from xy')
#     MysqlTill.uid_sql("insert into xy() value(7,'ggg')")





